package org.example.library.service;

import org.example.library.pojo.Book;
import org.example.library.pojo.User;
import org.example.library.pojo.dto.BookDto;
import org.example.library.repository.IBookRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService implements IBookService {
    @Autowired
    IBookRepository bookRepository;

    @Override
    public Book add(BookDto book) {
        Book bookPojo = new Book();
        BeanUtils.copyProperties(book,bookPojo);
        return bookRepository.save(bookPojo);
    }

    @Override
    public Book getBook(Integer bookId) {
        return bookRepository.findById(bookId).orElseThrow(() -> {
            throw new IllegalArgumentException("图书不存在，参数异常！");
        });
    }

    @Override
    public Book update(BookDto book) {
        Book bookPojo = new Book();
        BeanUtils.copyProperties(book,bookPojo);
        // 打印日志检查 userId 是否被正确复制
        System.out.println("Editing book with ID: " + bookPojo.getBookId());
        return bookRepository.save(bookPojo);
    }

    @Override
    public Book delete(Integer bookId) {
        bookRepository.deleteById(bookId);
        return null;
    }
}
